<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="customerAcc">
        <el-input v-model="queryParams.customerAcc" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="车牌号" prop="licensePlate">
        <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="状态" prop="whitelistStatus">
        <el-select v-model="queryParams.whitelistStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.parkingcar_whitelist_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['parkingcar:whitelist:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleUpdateBatch"
          v-hasPermi="['parkingcar:whitelist:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['parkingcar:whitelist:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['parkingcar:whitelist:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getWhitelistCarList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="whitelistCarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="白名单ID" align="center" prop="whitelistId" />
      <el-table-column label="用户账号" align="center" prop="customerAcc" />
      <el-table-column label="用户名称" align="center" prop="customerName" />

      <el-table-column label="车牌号" align="center" prop="licensePlate" />

      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.whitelistStatus" :active-value="0" :inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['parkingcar:whitelist:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getWhitelistCarList" />

    <!-- 添加或修改车辆白名单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        
        <el-form-item label="状态" prop="whitelistStatus">
          <el-radio-group v-model="form.whitelistStatus">
            <el-radio v-for="dict in dict.type.parkingcar_whitelist_status" :key="dict.value" :label="parseInt(dict.value)">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 传送门 -->
        <el-transfer
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请输入车牌号"
          :titles="['可选车牌', '已选车牌']"
          v-model="wholeCarList"
          :data="form.selectedCarList">
        </el-transfer>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWhitelist, getWhitelist, delWhitelist, addWhitelist, updateWhitelist } from "@/api/parkingcar/whitelist";
import { listCars } from "@/api/parkingcar/car";
export default {
  name: "Whitelist",
  dicts: ['parkingcar_whitelist_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licensePlate: null, // 添加车牌号查询参数
        customerAcc: null,
        customerName: null,
        whitelistId: null,
        carId: null,
        
      },
      // 传送门
      wholeCarList: [], // 可选车牌列表
      whitelistCarLisencePlate: [], // 已在白名单车牌号
      whitelistCarList:[],// 白名单车牌列表
      carPlateList:[],
      whitelistStatus: null,
      filterMethod(query, item) {
        return item.label.indexOf(query) > -1;
      },
      // 表单参数
      form: {
        whitelistId: null,
        selectedCarList: [], // 已选车牌号
        whitelistStatus: null,
      },
      // 表单校验
      rules: {
        //用户账号校验
        customerAcc: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        //用户名称
        customerName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        //车牌校验
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        whitelistStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }


    };
  },
  created() {
    // this.getWhitelistCarList();
  },
   mounted(){
      this.getWhitelistCarList();
      // this.getCarWhiteList();
  },
  watch: {
    whitelistList(newVal) {
      if (newVal.length > 0) {
        this.getCarWhiteList();
      }
    }
  },
  methods: {
    /** 查询车辆白名单管理列表 */
    getWhitelistCarList() {
      this.loading = true;
      listWhitelist(this.queryParams).then(response => {
        console.log("response", response)
        this.whitelistCarList = response.whiteList.rows;
        this.total = response.whiteList.total;
        this.wholeCarList = response.parkCarLIst || [];
        this.loading = false;
      });
    },
    // 查询所有车辆列表
    getCarList() {
      
      //   this.parkCarList.map(item => item.licensePlate);
      //   this.parkCarList.forEach(item => {
      //     const isOnWhite = this.whitelistList.some(infos => infos.licensePlate=== item.licensePlate);
      //     this.carList.push({
      //       label: item.licensePlate + "----" + item.customerName,
      //       key: item.licensePlate,
      //       disabled: isOnWhite
      //     });
      //   })
      //   // console.log(this.parkCarList,8888888888888)
      //   // console.log(this.whitelistList.length,333333333333333);
      //  if(!Array.isArray(this.value)){
      //   this.value = [];
      //  }
      //   this.whitelistList.forEach(item =>{
      //     this.$set(this.value,this.value.length,item.licensePlate);
          
      //   })
        
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        whitelistId: null,
        carId: null,
        selectedCarList: [], // 已选车牌列表
        customerAcc: null,
        customerName: null,
        whitelistStatus: null
      };
      wholeCarList = [], // 可选车牌列表
      whitelistCarLisencePlate = [], // 已在白名单车牌号
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getWhitelistCarList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => ({ key: item.carId, label: item.licensePlate, whitelistId: item.whitelistId }))
      console.log(this.ids)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    // 白名单状态修改
    handleStatusChange(row) {
      let text = row.whitelistStatus == "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"该车辆吗？').then(function () {
        const data = {
          whitelistId: row.whitelistId,
          whitelistStatus: row.whitelistStatus

        }
        return updateWhitelist(data);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.whitelistStatus = row.whitelistStatus === 0 ? 1 : 0;
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.loadCarList();
      this.open = true;
      this.title = "添加车辆白名单";
      this.getCarList();
    },
    /** 修改按钮操作 */
    handleUpdateBatch() {
      this.reset();
      this.carList = this.ids;
      this.open = true;
      this.title = "修改车辆白名单";
      this.getCarList();
    },
    handleUpdate(row) {
      this.reset();
      const whitelistId = row.whitelistId
      getWhitelist(whitelistId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆白名单";
        this.getCarList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let req = {}
          this.form.licensePlate.forEach(item => {
            const cars = this.carList.filter(e => {
              return e.key === item
            })
            console.log(cars)
            if (cars.length > 0 && cars[0].whitelistId && cars[0].whitelistId != null) {
              req = {
                whitelistId: cars[0].whitelistId,
                whitelistStatus: this.form.whitelistStatus
              }
              updateWhitelist(req).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getWhitelistCarList();
              });
            } else {
              req = {
                whitelistId: cars[0].whitelistId,
                whitelistStatus: this.form.whitelistStatus,
                carId: item,
              }
              addWhitelist(req).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getWhitelistCarList();
              });
            }
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const whitelistIds = row.whitelistId;
      this.$modal.confirm('是否确认删除车辆白名单管理编号为"' + whitelistIds + '"的数据项？').then(function () {
        return delWhitelist(whitelistIds);
      }).then(() => {
        this.getWhitelistCarList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parkingcar/whitelist/export', {
        ...this.queryParams
      }, `whitelist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
